package com.chixing.controller;

import com.chixing.service.OrderService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 马博
 * * @date 2021/10/18
 */
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @PostMapping("order/save")
    @ResponseBody
    @HystrixCommand(fallbackMethod = "saveOrderFail")
    public Object save(Integer userId,Integer proId){
        System.out.println("orderController userId:"+userId);
        System.out.println("orderController proId:"+proId);
        Map<String,Object> data=new HashMap<>();
        data.put("code",0);
        data.put("data",orderService.save(userId,proId));
        return data;
    }
    //方法名要与api方法一致
    private Object saveOrderFail(Integer userId,Integer proId){
        Map<String,Object> msg=new HashMap<>();
        msg.put("code",-1);
        msg.put("msg","抢购人数太多,你被挤出来了,请稍后重试");
        return msg;
    }
    @GetMapping("order/get")
    public Object get(){
        return "hello";
    }
}
